package eight_shangchuan;

import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.UUID;

public class ServerReaderThread extends Thread {
    private Socket s;

    public ServerReaderThread(Socket s) {
        this.s = s;
    }

    @Override
    public void run() {
        OutputStream os = null;
        try {
            DataInputStream dis = new DataInputStream(s.getInputStream());
            String suffix = dis.readUTF();
            System.out.println("Server has received a " + suffix);
            os = new FileOutputStream("mian\\src\\eight_shangchuan\\Copy\\" + UUID.randomUUID().toString() + suffix);
            byte[] bys = new byte[1024];
            int len;
            while ((len = dis.read(bys)) != -1) {
                os.write(bys, 0, len);
            }
            System.out.println("Save succeed!!!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
